/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db_vdumper.beans;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.opu.db_vdumper.domain.DColumn;
import org.opu.db_vdumper.domain.DTable;
import org.opu.db_vdumper.io.FileIO;
import org.opu.db_vdumper.resources.ConfigPath;
import org.opu.db_vdumper.services.BeanService;
import org.opu.db_vdumper.services.DomainService;
import org.opu.db_vdumper.services.ServiceFactory;
import org.opu.db_vdumper.ui.MainView;
import org.opu.db_vdumper.ui.component.area.model.CanvesModel;
import org.opu.db_vdumper.ui.component.tabel.models.TableModel;
import org.opu.db_vdumper.util.DbManager;
import org.opu.db_vdumper.util.Logger;
import org.opu.db_vdumper.util.monitors.ApplicationDataI18N;
import org.opu.db_vdumper.util.monitors.ApplicationDataI18NImpl;
import org.opu.db_vdumper.util.monitors.SimpleProgresMonitor;

/**
 *
 * @author yura
 */
public class ApplicationData {

    // data resoure
    private UserData data;
    private DbManager dbManager;
    
    // data procesor
    private ServiceFactory serviceFactory;
    private ApplicationDataI18N i18N;
    
    // data presentor
    private CanvesModel canvesModel;

    // main view
    private MainView mainView;

    public ApplicationData(MainView mainView) {
        this.serviceFactory = new ServiceFactory();
        this.canvesModel = new CanvesModel();
        this.i18N = new ApplicationDataI18NImpl();

        this.mainView = mainView;
        this.mainView.setCanvesModel(canvesModel);

        Logger.getInstance().info(ApplicationData.class, "canves model "
                + canvesModel.hashCode());
    }

    public void setDbData(UserData data) throws ClassNotFoundException,
            SQLException {

        if (data != null) {
            setDbManager(new DbManager(data));
        } else {
            this.data = null;
            this.dbManager = null;
            this.canvesModel.setTablesBean(new ArrayList<TableBean>(0));
        }
    }

    public UserData getDbData() {
        return data;
    }

    public void setDbManager(DbManager dbManager) throws SQLException {
        if (dbManager == null){
            throw new NullPointerException("Database connect can't be null");
        }

        if (this.dbManager != null){
            this.dbManager.closeConnect();
        }

        this.dbManager = dbManager;
        this.data = dbManager.getUserData();

        Logger.getInstance().info(this, "Set database data " + this.dbManager);
    }

    public void reset(SimpleProgresMonitor monitor) throws SQLException {
        Logger.getInstance().info(this, "Reset application data");

        monitor.setNoteAndProgress(this.i18N.getDomainGet(), 100);
        DomainService domainServ = serviceFactory.getDomainService(dbManager);

        monitor.setNoteAndProgress(this.i18N.getDomainColumns(), 300);
        Map<DTable, Map<String, DColumn>> columns = domainServ.getColumns();
        monitor.setNoteAndProgress(this.i18N.getDomainTables(), 400);
        Map<String, DTable> tables = domainServ.getTables();

        monitor.setNoteAndProgress(this.i18N.getBeanGet(), 500);
        BeanService beanService = serviceFactory.getBeanService();
        monitor.setNoteAndProgress(this.i18N.getBeanTables(), 600);
        List<TableBean> beans = beanService.getTables(tables, columns);

        monitor.setNoteAndProgress(this.i18N.getLog(), 700);
        for (TableBean tb : beans) {
            Logger.getInstance().debug(this, tb);
        }

        monitor.setNoteAndProgress(this.i18N.getRepaint(), 900);
        canvesModel.setTablesBean(beans);
        monitor.setProgress(99);
        Logger.getInstance().debug(this, "Reset application data ended");
    }

    public DbManager getDbManager() {
        return dbManager;
    }

    public CanvesModel getCanvesModel() {
        return canvesModel;
    }

    public ServiceFactory getServiceFactory() {
        return serviceFactory;
    }

    public ApplicationDataI18N getI18N() {
        return i18N;
    }

    public MainView getMainView() {
        return mainView;
    }

}
